package com.njit.office.controller;

import com.njit.office.dataobject.LoginUser;
import com.njit.office.service.EmailService;
import com.njit.office.service.RedisService;
import com.njit.office.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class AccountController {

    @Resource
    private EmailService emailService;

    @Resource
    private UserService userService;

    private Random random = new Random();
    @Resource
    RedisService redisService;


    private Map<String, String> mailInfoMap = new HashMap<>();




    @GetMapping("/sendEmail")
    @ResponseBody
    public Map<String,String> sendEmail(@RequestParam("mail")String email){

        Map<String, String> result = new HashMap<>();


        //判断是否已经注册
        LoginUser loginUser = userService.getUserWithMail(email);
        if (loginUser.getEmail()!=null&&!loginUser.getEmail().equals("")) {
            result.put("state", "failed");
            result.put("msg", "邮箱已注册");
            return result;
        }

        //发送激活码
        String ran = String.valueOf(1000+random.nextInt(8999));
        emailService.send(email,"您的注册请求我们已经收到","您好，您本次注册所需的激活码为："+ran);
        mailInfoMap.put(email, ran);

        result.put("state", "ok");
        result.put("msg", "发送激活邮件成功");
        return result;
    }

    @PostMapping("/reg")
    @ResponseBody
    public Map<String, Object> register(@RequestBody Map<String,String> map){

        Map<String, Object> result = new LinkedHashMap<>();

        String mail = map.get("mail");
        String code = map.get("code");
        String pwd = map.get("pwd");

        //校验code
        if (mailInfoMap.get(mail) == null || !mailInfoMap.get(mail).equals(code)) {
            result.put("state","failed");
            result.put("msg", "激活码不正确!");

            return result;
        }
        LoginUser loginUser = userService.register(mail, pwd);
        result.put("state", "ok");
        result.put("msg", "注册成功");
        result.put("data", loginUser);
        return result;
    }

    @PostMapping("/login")
    @ResponseBody
    public Map<String,Object> login(@RequestBody Map<String,String> map, HttpServletRequest request, HttpServletResponse response){

        Map<String, Object> result = new LinkedHashMap<>();

        String email = map.get("email");
        String pwd = map.get("pwd");

        //判断邮箱是否存在
        LoginUser loginUser = userService.getUserWithMail(email);
        if (loginUser.getEmail()==null||loginUser.getEmail().equals("")) {
            result.put("state", "failed");
            result.put("msg", "邮箱不存在");
        }else{
            //检查密码
            loginUser = userService.login(email, pwd);
            if (loginUser.getEmail()==null||loginUser.getEmail().equals("")) {
                result.put("state", "failed");
                result.put("msg", "密码错误");
            }else{
                HttpSession session = request.getSession();
                session.setAttribute("userId", loginUser.getUserId());
                redisService.set("loginUser:" + loginUser.getUserId(), session.getId());

                Cookie cookie = new Cookie("UserId", loginUser.getUserId());
                cookie.setMaxAge(5 * 60);
                response.addCookie(cookie);
                result.put("state", "ok");
                result.put("msg", "登录成功");
                result.put("data", loginUser);
            }
        }
        return result;

    }



}
